/*
 * MolecularFormula.java
 * 
 * Generates a multiplier for the empirical formula in order to generate the
 * Molecular Formula based on its Molar Mass
 * 
 * Author: Andres Ruiz
 * License: see License.txt
 */
public class MolecularFormula
{
	private int[] NumOfElements;
	private String[] Elements;
	private double[] AtomicMasses;
	private double MolarMass;
	private double empMolarMass;
	private double Mult;

	PeriodicTableAcess Search;

	InfoStringParse AtomicMass;// fetches needed information
	
	MolarMass molarMass;

	public MolecularFormula(String[] elements, int[] numofelement, double molarMass)
	{
		Elements = elements;
		NumOfElements = numofelement;
		MolarMass = molarMass;
		AtomicMasses = new double[Elements.length];

		// run the internal methods
		getAtomicMass();
		empiricalFormulaMass();
		empiricMult();
	}

	private void getAtomicMass()// gets the necesary info from InfoStringParse
								// method
	{
		for (int i = 0; i < Elements.length; i++)
		{
			Search = new PeriodicTableAcess(Elements[i]);
			String results = Search.getSearchResult();
			AtomicMass = new InfoStringParse(results);
			AtomicMasses[i] = AtomicMass.getAtomicMass();
		}
	}

	private void empiricalFormulaMass()// adds all the elements to get the
										// empirical molar mass
	{
		molarMass = new MolarMass(AtomicMasses);
	}

	private void empiricMult()// generates the multiplier
	{
		Mult = MolarMass / empMolarMass;
	}

	public double getResult()// sends results to other methods
	{
		return Mult;
	}

	public void printResult()// prints the results in the screen
	{
		System.out.println("Empirical Formula Molar Mass:" + empMolarMass);
		System.out.println(MolarMass + " / " + empMolarMass + " = " + Mult);
		System.out.println();
		System.out.println("You will find the real Molecular Formula by multipling by: " + Mult);
		System.out.println("Note these are aproximations");
		System.out.println("Round the multiplier accodingly.");
		System.out.println();
	}
}
